/************************************************************************
 *File Name: main.c
 *Author: wky
 *Created Time: 2023年06月20日 星期二 20时32分19秒
 *Describtion: 网盘客户端 
 ************************************************************************/
#include "client.h"
#include "client_transfile.h"

int readName(char *name);
int readCode( char* code);

int main(int argc ,char* argv[])
{
    // ./client ip iport 
    ARGS_CHECK(argc,3);

    //tcp连接服务端
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);

    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = inet_addr(argv[1]);
    addr.sin_port = htons(atoi(argv[2]));
    int ret = connect(sockfd, (struct sockaddr*)&addr, sizeof(addr));
    ERROR_CHECK(ret, -1, "connect");

    //接收客户的账户名和密码
    char name[50];
    readName(name);
    printf("name = %s, len = %ld\n",name,strlen(name));

    //发送给服务端
    train_t train;
    bzero(&train,sizeof(train));
    train.length = strlen(name);
    strcpy(train.data,name);
    printf("train.length = %d, train.data = %s\n",train.length, train.data);
    send(sockfd, &train, sizeof(int)+train.length, MSG_NOSIGNAL);

    //接收客户的密码
    char code[50];
    readCode(code);
    printf("code = %s, len = %ld\n",code,strlen(code));

    //发送给服务端
    bzero(&train,sizeof(train));
    train.length = strlen(code);
    strcpy(train.data, code);
    printf("train.length = %d, train.data = %s\n",train.length, train.data);
    send(sockfd, &train, sizeof(int)+train.length, MSG_NOSIGNAL);

    //接收服务端验证结果
    int is_true;
    recv(sockfd, &is_true, sizeof(int), 0);
    //recv(sockfd, &is_true,train.length, 0 );
    printf("is_true = %d\n",is_true);
    if(is_true != 1)
    {//密码不正确
        printf("密码错误\n");
        close(sockfd);
        return 0;
    }

    printf("欢迎\n");

    int epfd = epoll_create(1);
    addEpoll(epfd, sockfd);
    addEpoll(epfd, STDIN_FILENO);

    struct epoll_event readySet[10];
    order_t ord;
    char sentence[512];
    ssize_t sret;
    while(1)
    {
        int readyNUm = epoll_wait(epfd, readySet, 10, -1);
        for(int i = 0; i<readyNUm; ++i)
        {
            if(readySet[i].data.fd == STDIN_FILENO)
            {//接收用户命令
                bzero(sentence, sizeof(sentence));
                bzero(&ord, sizeof(ord));
                sret = read(STDIN_FILENO, sentence, sizeof(sentence));               
                if(sret == 0)
                {//用户退出机制 暂定
                    close(sockfd);
                    return 0;
                }
                
                ret =  isOrder( sentence, &ord);
                if(ret == -1){
                    break;
                }

                send(sockfd, &ord, sizeof(ord), MSG_NOSIGNAL);
                switch(ord.num)
                {
                case GETS:
                    printf("下载文件\n");
                    recvFile(ord.opt, sockfd);
                    break;
                case PUTS:
                    printf("上传文件\n");
                    sendFile(ord.opt, sockfd);
                    break;
                case PWD:
                    printf("PWD\n");
                    pwd_client(sockfd);
                    break;
                case LS:
                    printf("LS\n");
                    lsClient(sockfd);
                    break;
                //case 88:
                //    close(sockfd);
                //    return 0;
                }
            }

        }
    }


    return 0;
}

int readName(char *name )
{
    bzero(name,sizeof(name));

    printf("用户名: ");
    scanf("%s",name);

    return 0;
}
int readCode(char* code)
{

    bzero(code,sizeof(code));
    char *p = getpass("密码：");
    strcpy(code,p);
    return 0;
}

int creatTrain(const char* buf, train_t * train){
    train->length = strlen(buf);
    for(int i =0 ;i< train->length;++i){
        train->data[i++] = buf[i++];
    }
    return 0;
}
